package com.ermao.learnWebmagic.processor_2;

import com.ermao.learnWebmagic.config.BilibiliAPIEnum;
import com.ermao.learnWebmagic.config.Config;
import com.ermao.learnWebmagic.entity.Video;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

public class VideoPageProcessor implements PageProcessor {

	private static final CopyOnWriteArrayList<Video> result = new CopyOnWriteArrayList<>();


	@Override
	public void process(Page page) {
		new VideoPageHandler().handle(page, result);
	}

	@Override
	public Site getSite() {
		return Site.me().setRetryTimes(Config.RETRY_TIMES).setSleepTime(Config.SLEEP_TIME).setUserAgent(Config.USER_AGENT);
	}

	public static void start(String keyword) {
		Spider spider = Spider
				.create(new VideoPageProcessor())
				.addUrl(BilibiliAPIEnum.getAPI(BilibiliAPIEnum.SEARCH, keyword))
				.thread(5);
		spider.run();
		spider.close();
		storeCSV();
	}

	public static void storeCSV() {
		try (CSVPrinter printer = new CSVPrinter(new FileWriter(Config.VIDEO_CSV_FILE_NAME, true), CSVFormat.DEFAULT.withHeader(Config.VIDEO_CSV_FILE_HEADER))) {
			for (Video video : result) {
				printer.printRecord(video.getVideo_up_mid(), video.getVideo_title(), video.getVideo_play(), video.getVideo_created(), video.getVideo_like()
						, video.getVideo_coin(), video.getVideo_fav(), video.getVideo_share(), video.getVideo_reply(), video.getVideo_danmaku(), video.getVideo_tags());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
